function getCurrentTimestamp () {
    return Math.round(new Date().getTime() / 1000);
}

function timestampConvert (fmt, timestamp) {
    const cnDigital = ['日', '一', '二', '三', '四', '五', '六'];
    const date = new Date(timestamp);
    const o = {
        'M+': date.getMonth() + 1, // 月份
        'd+': date.getDate(), // 日
        'h+': date.getHours(), // 小时
        'm+': date.getMinutes(), // 分
        's+': date.getSeconds(), // 秒
        'w+': date.getDay(), // 周几(阿拉伯数字)
        'W+': cnDigital[date.getDay()], // 周几(中文)
        'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
        S: date.getMilliseconds() // 毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
    for (const k in o) {
        if (new RegExp('(' + k + ')').test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)));
    }
    return fmt;
}

/**
 * 获取昨天凌晨的时间戳
 * @returns {number}
 */
function getBeforeDayTimestamp (num) {
    num = num || -1;
    let yesTimestamp = new Date(getDay(num) + 'T00:00:00');
    return yesTimestamp.getTime() / 1000;
}

function getDay (num) {
    let today = new Date();
    let nowTime = today.getTime();
    let ms = 24 * 3600 * 1000 * num;
    today.setTime(parseInt(nowTime + ms));
    let oYear = today.getFullYear();
    let oMoth = (today.getMonth() + 1).toString();
    if (oMoth.length <= 1) oMoth = '0' + oMoth;
    let oDay = today.getDate().toString();
    if (oDay.length <= 1) oDay = '0' + oDay;
    return oYear + '-' + oMoth + '-' + oDay;
}

module.exports = {
    getCurrentTimestamp,
    timestampConvert,
    getBeforeDayTimestamp
};
